package com.baicaoyuan.web.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * alibaba fastjson utils
 *
 * @author Chen
 * @version 2017-05-17
 * @see https://github.com/alibaba/fastjson/wiki
 */
public final class FastJsonUtils {

    /**
     * 将 JavaBean转化为Json格式
     *
     * @param obj
     * @return
     */
    public static String object2JsonString(Object obj) {
        String jsonString = null;
        try {
            jsonString = JSON.toJSONString(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonString;
    }

    /**
     * 将json字符串解析为一个 JavaBean
     *
     * @param jsonString
     * @param cls
     * @return
     */
    public static <T> T jsonString2Object(String jsonString, Class<T> cls) {

        T t = null;
        try {
            t = JSON.parseObject(jsonString, cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * 将 List<JavaBean> 及 List<String> 转化为Json格式
     *
     * @param <T>
     * @param List<T> objArray
     * @return
     */
    public static <T> String objectArray2JsonString(List<T> objArray) {
        String jsonString = null;
        try {
            jsonString = JSON.toJSONString(objArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonString;
    }

    /**
     * 将json字符串 解析成为一个 List<JavaBean> 及 List<String>
     *
     * @param jsonString
     * @param cls
     * @return
     */
    public static <T> List<T> jsonString2ObjectArray(String jsonString, Class<T> cls) {
        List<T> list = new ArrayList<T>();
        try {
            list = JSON.parseArray(jsonString, cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 将jsonString 解析成 List<Map<String,Object>>
     *
     * @param jsonString
     * @return
     */
    public static List<Map<String, Object>> getListMap(String jsonString) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        try {
            // 两种写法
            // list = JSON.parseObject(jsonString, new
            // TypeReference<List<Map<String, Object>>>(){}.getType());

            list = JSON.parseObject(jsonString,
                    new TypeReference<List<Map<String, Object>>>() {
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 将Bean对象转换成json字符串并写入指定文件
     *
     * @param BeanName.Class
     * @param file
     * @return
     */
    public static <T> void writeJsonToFile(T t, File file) throws IOException {
        String jsonStr = JSONObject.toJSONString(t, SerializerFeature.PrettyFormat);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        bw.write(jsonStr);
        bw.close();
    }

    /**
     * 将Bean对象转换成json字符串并写入指定路径文件
     *
     * @param BeanName.Class
     * @param filename       文件完整路径
     * @return
     */
    public static <T> void writeJsonToFile(T t, String filename) throws IOException {
        writeJsonToFile(t, new File(filename));
    }

    /**
     * 从指定文件读取Json内容，并转换成对象
     *
     * @param BeanName.Class
     * @param file
     * @return
     */
    public static <T> T readJsonFromFile(Class<T> cls, File file) throws IOException {
        StringBuilder strBuilder = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        String line = null;
        while ((line = br.readLine()) != null) {
            strBuilder.append(line);
        }
        br.close();
        return JSONObject.parseObject(strBuilder.toString(), cls);
    }

    /**
     * 从指定文件路径读取Json内容，并转换成对象
     *
     * @param BeanName.Class
     * @param filename
     * @return
     */
    public static <T> T readJsonFromFile(Class<T> cls, String filename) throws IOException {
        return readJsonFromFile(cls, new File(filename));
    }

    public static <T> T readJsonFromFile(TypeReference<T> typeReference, File file) throws IOException {
        StringBuilder strBuilder = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        String line = null;
        while ((line = br.readLine()) != null) {
            strBuilder.append(line);
        }
        br.close();
        return JSONObject.parseObject(strBuilder.toString(), typeReference);
    }

    public static <T> T readJsonFromFile(TypeReference<T> typeReference, String filename) throws IOException {
        return readJsonFromFile(typeReference, new File(filename));
    }

    /**
     * 将jsonString 解析成 JSONObject对象
     *
     * @param jsonString
     * @return
     */
    public static JSONObject parseJSONObject(String jsonString) {
        JSONObject obj = null;
        try {
            obj = JSON.parseObject(jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

}